// Copyright 2009, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.urchin.dataapi.rest.adminservice;

/**
 * This code sample
 *  - retrieves list of accounts for specified user
 *  - retrieves list of profiles for specified user and [optional] account
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.google.urchin.dataapi.soap.adminservice.AdminserviceStub;

public class AdminService {
  // Provide login information
  private static final String login = "URCHIN_LOGIN";
  private static final String password = "URCHIN_PASSWORD";
  private static final String serviceURL = "http://URCHINHOST:URCHINPORT/services/v1/adminservice/";

  public static void main(String[] args) {
    try {
      GetAccountList();
      GetProfileList();
    }
    catch(Exception e) {
      e.printStackTrace(); 
    }
  }
  
  /*
   * Retrieve list of accounts for given user
   */
  public static void GetAccountList() throws Exception {
    URL service = new URL(serviceURL + "accounts/" + "?login=" + login + "&password=" + password);

    // Create the buffering character-input stream
    BufferedReader in = new BufferedReader(new InputStreamReader(service.openStream()));

    // Create XPath for retrieving data
    XPathFactory fact = XPathFactory.newInstance();
    XPath xp = fact.newXPath();
    InputSource is = new InputSource(in);

    // set namespace context
    xp.setNamespaceContext(new UrchinNamespaceContext());

    // retrieve list of accounts
    NodeList accounts = (NodeList)xp.evaluate("/tns:getAccountListResponse/account", is, XPathConstants.NODESET);

    // dump each account
    System.out.println("adminservice/accounts/");
    for(int i = 0; i < accounts.getLength(); i++){
      System.out.print("account id: \"" + xp.evaluate("accountId/text()", accounts.item(i)) + "\", ");
      System.out.print("account name: \"" + xp.evaluate("accountName/text()", accounts.item(i)) + "\", ");
      System.out.print("contact name: \"" + xp.evaluate("contactName/text()", accounts.item(i)) + "\", ");
      System.out.print("e-mail: \"" + xp.evaluate("emailAddress/text()", accounts.item(i)) + "\", ");
      System.out.println("phone number: \"" + xp.evaluate("phoneNumber/text()", accounts.item(i)) + "\"");
    }
    in.close();
  }

  /*
   * Retrieve list of profiles for specified user and [optional] account
   */
  public static void GetProfileList() throws Exception {
    URL service = new URL(serviceURL + "profiles/" + "?login=" + login + "&password=" + password);

    // Create the buffering character-input stream
    BufferedReader in = new BufferedReader(new InputStreamReader(service.openStream()));

    // Create XPath for retrieving data
    XPathFactory fact = XPathFactory.newInstance();
    XPath xp = fact.newXPath();
    InputSource is = new InputSource(in);

    // set namespace context
    xp.setNamespaceContext(new UrchinNamespaceContext());

    // retrieve list of accounts
    NodeList profiles = (NodeList)xp.evaluate("/tns:getProfileListResponse/profile", is, XPathConstants.NODESET);

    // dump each account
    System.out.println("adminservice/profiles/");
    for(int i = 0; i < profiles.getLength(); i++){
      System.out.print("profile id: \"" + xp.evaluate("profileId/text()", profiles.item(i)) + "\", ");
      System.out.print("profile name: \"" + xp.evaluate("profileName/text()", profiles.item(i)) + "\", ");
      System.out.print("account id: \"" + xp.evaluate("accountId/text()", profiles.item(i)) + "\", ");
      System.out.println("account name: \"" + xp.evaluate("accountName/text()", profiles.item(i)) + "\"");
    }
    in.close();
  }
}

/**
 * Urchin Data API namespace context implementation
 */
class UrchinNamespaceContext implements NamespaceContext {
  public String getNamespaceURI(String prefix) {
    if (prefix == null) throw new NullPointerException("Null prefix");

    if (prefix.equals("tns")) return "https://urchin.com/api/urchin/v1/";
    else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;

    return XMLConstants.NULL_NS_URI;
  }

  public String getPrefix(String namespaceURI) {
    throw new UnsupportedOperationException();
  }

  public Iterator getPrefixes(String namespaceURI) {
    throw new UnsupportedOperationException();
  }
}
